package msu.erdc.hci;

import msu.erdc.hci.customObjects.ContactObject;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactDetail extends Fragment {

	Button editContact;
	ImageView callButton;
	ImageView mobilecallButton;
	ImageView emailButton;
	ImageView gpsButton;
	ImageView textButton;
	TextView name;
	TextView officePhone;
	TextView mobilePhone;
	TextView email;
	TextView address1;
	TextView address2;
	TextView category;
	int contactIndex;
	ContactObject clickedContact;
	//public static String thisaddress;
	//public static String afname;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		
		View view =  inflater.inflate(R.layout.completecontact_detail, container, false);
		
		name = (TextView) view.findViewById(R.id.nameText);
		editContact = (Button) view.findViewById(R.id.editButton);
		name = (TextView) view.findViewById(R.id.myName);
		officePhone = (TextView) view.findViewById(R.id.officephoneText);
		mobilePhone = (TextView) view.findViewById(R.id.mycellphone);
		address1 = (TextView) view.findViewById(R.id.address1Text);
		address2 = (TextView) view.findViewById(R.id.address2Text);
		email = (TextView) view.findViewById(R.id.emailText);
		category = (TextView) view.findViewById(R.id.myLawyer);
		
		mobilePhone.setText(clickedContact.mobile);
		name.setText(clickedContact.getName());
		officePhone.setText(clickedContact.phone);
		address1.setText(clickedContact.street);
		address2.setText(clickedContact.cityState);
		email.setText(clickedContact.emailaddress);
		category.setText(clickedContact.category);
		
		 editContact.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					EditContactDetail editContactFrag = new EditContactDetail();
					Bundle arguments = new Bundle();
					arguments.putInt("index", contactIndex);
					editContactFrag.setArguments(arguments);
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					ft.replace(android.R.id.content, editContactFrag, "contactdetailsedit");
					ft.addToBackStack(null);
					ft.commit();
				}});
		 
		 callButton = (ImageView) view.findViewById(R.id.callButton);
		 callButton.setOnClickListener(new OnClickListener(){
			 @Override
				public void onClick(View arg0) {
					
					call();
				    
				}});
		 
		 mobilecallButton = (ImageView) view.findViewById(R.id.mobilecallButton);
		 mobilecallButton.setOnClickListener(new OnClickListener(){
			 @Override
				public void onClick(View arg0) { 
					
					call();
				    
				}});
		 
		 emailButton = (ImageView) view.findViewById(R.id.emailButton);
		 emailButton.setOnClickListener(new OnClickListener(){
			 @Override
				public void onClick(View arg0) { 
					
				 Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
					//emailIntent.setType("text/plain");
					emailIntent.setType("message/rfc822");
					emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Accident Info");
					emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "email body");
					startActivity(Intent.createChooser(emailIntent, "Send Email:")); 
				    
				}});
		 
		 textButton = (ImageView) view.findViewById(R.id.textButton);
		 textButton.setOnClickListener(new OnClickListener(){
			 @Override
				public void onClick(View arg0) { 
					
				 Uri uri = Uri.parse("12345");
					Intent textIntent = new Intent(Intent.ACTION_SENDTO,uri);
					textIntent.putExtra("sms+body", "sms text");
					textIntent.setType("vnd.android-dir/mms-sms"); 
					startActivity(Intent.createChooser(textIntent, "Send Text:"));
				    
				}});
		 
		 
		 gpsButton = (ImageView) view.findViewById(R.id.gpsButton);
		 gpsButton.setOnClickListener(new OnClickListener(){
			 @Override
				public void onClick(View arg0) { 
					
				 Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  
						 Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345")); 
						 startActivity(intent); 
				    
				}});
		 
		return view;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		contactIndex = 0;
		Bundle arguments = getArguments();
		if(arguments!=null)
		{
			contactIndex = arguments.getInt("index");
		}
		clickedContact = ((Global)getActivity().getApplication()).contacts.get(contactIndex);
	}
	
	 private void call(){
	    	
	        try {

	       Intent callIntent = new Intent(Intent.ACTION_CALL);
	       callIntent.setData(Uri.parse("tel:555-555-5555"));
	       startActivity(callIntent);
	       } catch (ActivityNotFoundException e) {
	       Log.e("dialing", "Call failed", e);
	       }
	    }
	 
	
}